package com.example.myapplication2;

import java.lang.reflect.Array;
import java.net.InetSocketAddress;
import java.util.*;
import java.lang.*;
import java.io.*;
import java.nio.*;
import java.nio.channels.*;
import java.security.MessageDigest;

public class helpFunc {
    static boolean hasDam = true;
    static int damLen = 2;
    static int udpReadLen = 65536;
    static int tcpReadLen = 65536;
    static int con_getNum = 10;
    static int con_connBufSize = 200 * 1024;
    static int con_timeoutInternal = 20;
    static int con_maxConn = 500;
    static int con_cachePerConn = 200 * 1024;
    static int server_port = 8895;
    static byte[] con_key = new String("tr").getBytes();
    static byte[] con_serverKey = new String("hfeiffcc").getBytes();
    static int con_packEffectTime = 10;
    static int serverSenLen = 1450;
    static int cliSenLen = 1450;
    static int serverPackLen = 0;
    static int cliPackLen = 0;
    static boolean jj = calLen();
    static int serverStaPerSecond = 40;
    static int con_ssPort = 8080;
    static int timeoutTime = 1000;
    static int maxNewSockPerSecond = 50;
    static int con_portsNum = 100;
    static int con_maxPackRecv = 300;
    static int newPortsControlPort, bufControlPort;
    static String serverIp = "42.51.28.250";
    static int con_localPort = 9995;
    static int con_timeoutPlus = 80;
    static byte[] fileContent = new byte[0];
static  int damMaxLen = 0;
    static int bool2int(boolean a) {
        if (a) return 1;
        return 0;
    }

    static void addMid(LinkedList<byte[]> l, byte[] b, int maxSize) {
        boolean in = false;
        for (int lo = 0; lo < l.size(); lo++) {
            if (b.length > l.get(lo).length + 25) {
                l.add(lo, b);
                in = true;
                break;
            }
        }
        if (l.size() > maxSize)
            l.removeLast();
        if (!in && l.size() < maxSize)
            l.add(b);
    }

    static boolean calLen() {
        damMaxLen = 0;
        for (String st : defaultChi.damList) {
            if (st.length() * 1.5 > damMaxLen) damMaxLen = (int) (st.length() * 1.5);
        }
        int keyLen = (helpFunc.con_connBufSize / 1024) % 10 + 2;
        serverPackLen = ((serverSenLen - 20 - (bool2int(hasDam) * damMaxLen)) / con_getNum) - 2;
        cliPackLen = cliSenLen - 35 - keyLen - (bool2int(hasDam) * damMaxLen) - 4 * con_getNum;
        return true;
    }

    public static int b2i(byte a) {
        if (a >= 0) return a;
        return a + 256;
    }

    private static final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();

    public static String bytesToHex(byte[] bytes) {
        char[] hexChars = new char[bytes.length * 2];
        for (int j = 0; j < bytes.length; j++) {
            int v = bytes[j] & 0xFF;
            hexChars[j * 2] = HEX_ARRAY[v >>> 4];
            hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F];
        }
        return new String(hexChars);
    }

    public static boolean arrayEqu(byte[] a, byte[] b) {
        return Arrays.equals(a, b);
    }

    public static int bindAnyPort(ServerSocketChannel so) throws Exception {
        InetSocketAddress add = new InetSocketAddress("localhost", 0);
        so.bind(add);
        InetSocketAddress tt = (InetSocketAddress) so.getLocalAddress();
        return tt.getPort();
    }

    public static byte[] tcpRead(SocketChannel so) throws Exception {
        ByteBuffer bu = ByteBuffer.allocate(helpFunc.tcpReadLen);
        int bytesRead = so.read(bu);
        byte[] temp = new byte[bytesRead];
        System.arraycopy(bu.array(), 0, temp, 0, bytesRead);
        return temp;
    }

    public static byte[] tcpWrite(SocketChannel so, byte[] dd) throws Exception {
        ByteBuffer temp = ByteBuffer.wrap(dd);
        so.write(temp);
        byte[] r = new byte[temp.remaining()];
        temp.get(r, 0, r.length);
        return r;
    }

    public static byte[] udpRead(DatagramChannel so) throws Exception {

        ByteBuffer dd = ByteBuffer.allocate(helpFunc.udpReadLen);
        so.receive(dd);
        byte[] j = new byte[dd.position()];
        System.arraycopy(dd.array(), 0, j, 0, dd.position());
        return j;
    }

    public static void udpWrite(DatagramChannel so, byte[] dd, InetSocketAddress add) throws Exception {
        ByteBuffer bb = ByteBuffer.wrap(dd);
        so.send(bb, add);
    }

    public static byte[] getuuid_byte() throws Exception {
        return MessageDigest.getInstance("MD5").digest(UUID.randomUUID().toString().getBytes());
    }

    public static void print(String a) {
        System.out.println(a);
    }

    public static byte[] readFile(String path, int l) throws Exception {
        File file = new File(path);
        FileInputStream in = new FileInputStream(file);
        byte[] bytes = new byte[l];
        in.read(bytes, 0, l);
        in.close();
        return bytes;
    }

    public static void writeFile(String path, byte[] bytes) throws Exception {
        File file = new File(path);
        FileOutputStream stream = new FileOutputStream(file);
        stream.write(bytes);
        stream.close();
    }
}
